package com.WFinstitute.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.WFinstitute.domain.News;
import com.WFinstitute.domain.Test;
import com.WFinstitute.service.NewsService;
import com.WFinstitute.util.ImgUtil;
import com.WFinstitute.util.Page;
import com.WFinstitute.util.TimeUtil;
import com.WFinstitute.util.TrimHtmlUtil;

/**
 * 
 * @author 匹夫
 * @date 2016-7-23 下午5:46:56
 * @function 新闻后台管理
 */
@Controller
@RequestMapping("/news")
public class NewsBackController {
	@Resource
	private NewsService  newsService;
	
	/**
	 * 【后台页面】添加新闻
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	public void add(HttpServletResponse response,HttpServletRequest request){
		String id = request.getParameter("id");
		try {
		JSONObject jo=new JSONObject();
		String title = request.getParameter("title");
		String remark = request.getParameter("remark");
		String content = request.getParameter("content");
		if(request.getParameter("id")!=null&&!request.getParameter("id").equals("")){
			int a = Integer.parseInt(id);
			News newsupdate = newsService.findNewsById(a);
			newsupdate.setContent(content);
			newsupdate.setRemark(remark);
			newsupdate.setTitle(title);
			newsupdate.setCreatetime(TimeUtil.getCurrentTime());
			newsService.updateNews(newsupdate);
		}else{
			News news=new News();
			news.setContent(content);
			news.setRemark(remark);
			news.setTitle(title);
			news.setCreatetime(TimeUtil.getCurrentTime());
		    newsService.addNews(news);
		}
		jo.put("state", "0");
		response.getWriter().write(jo.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 【后台页面】新闻分页列表界面
	 * @param response
	 * @param request
	 * @return
	 */
	        @ResponseBody
			@RequestMapping(value="/list")
			public  Map<String, Object>  userlist(HttpServletResponse response,HttpServletRequest request){
				JSONObject json =new JSONObject(); 
				//获取页面传来每页显示条数
		        int row=request.getParameter("rows")!=null?Integer.valueOf(request.getParameter("rows")):10;
		        //获取页面传来当前页码
		        int page=request.getParameter("page")!=null?Integer.valueOf(request.getParameter("page")):1;
		        //计算一共有多少条
		        int count = (int)newsService.count();
				Page<News> pageList = newsService.findNewsList(row, page, "");
				List<News> newsList = pageList.getList();
				List<News> newsListTemp = new ArrayList<News>();
				for (News news : newsList) {
					news.setUrl(news.getContent());
					news.setContent(TrimHtmlUtil.delHTMLTag(news.getContent()));
					//把没有处理的标签的content内容放到url只是单纯的为了显示内容而已
					news.setUrl(news.getContent());
					newsListTemp.add(news);
				}
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("rows", newsListTemp);
				result.put("total", count);
				return result;
			}
}
